home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / dev / src / expat-src.lha / expat-1.95.2 / examples / outline.c < prev   
Encoding:
C/C++ Source or Header  |  2001-11-25  |  2.6 KB  |  127 lines

  1. /*****************************************************************
  2.  * outline.c
  3.  *
  4.  * Copyright 1999, Clark Cooper
  5.  * All rights reserved.
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the license contained in the
  9.  * COPYING file that comes with the expat distribution.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14.  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  15.  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  16.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  17.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18.  *
  19.  * Read an XML document from standard input and print an element
  20.  * outline on standard output.
  21.  */
  22.  
  23.  
  24. #include <stdio.h>
  25. #ifndef AMIGA
  26. #include <expat.h>
  27. #else
  28. #include <exec/types.h>
  29. #include <exec/memory.h>
  30. #include <expat/expat.h>
  31. #include <proto/exec.h>
  32. #include <proto/expat.h>
  33.  
  34. struct Library *ExpatBase = NULL;
  35.  
  36. #endif
  37.  
  38. #define BUFFSIZE    8192
  39.  
  40. char Buff[BUFFSIZE];
  41.  
  42. int Depth;
  43.  
  44.  
  45. static void
  46. start(void *data, const char *el, const char **attr) {
  47.   int i;
  48.  
  49.   for (i = 0; i < Depth; i++)
  50.     printf("  ");
  51.  
  52.   printf("%s", el);
  53.  
  54.   for (i = 0; attr[i]; i += 2) {
  55.     printf(" %s='%s'", attr[i], attr[i + 1]);
  56.   }
  57.  
  58.   printf("\n");
  59.   Depth++;
  60. }  /* End of start handler */
  61.  
  62. static void
  63. end(void *data, const char *el) {
  64.   Depth--;
  65. }  /* End of end handler */
  66.  
  67. int
  68. main(int argc, char *argv[]) {
  69.   XML_Parser p;
  70. #ifdef AMIGA
  71.   ExpatBase = (APTR) OpenLibrary("expat.library", 0);
  72.   if(!ExpatBase)
  73.   {
  74.     puts("\nCouldn't open expat.library\n");
  75.     exit(20);
  76.   }
  77. #endif
  78.   
  79.   p = XML_ParserCreate(NULL);
  80.   if (! p) {
  81.     fprintf(stderr, "Couldn't allocate memory for parser\n");
  82. #ifdef AMIGA  
  83.       CloseLibrary((APTR) ExpatBase);;
  84. #endif
  85.     exit(-1);
  86.   }
  87.  
  88.   XML_SetElementHandler(p, start, end);
  89.  
  90.   for (;;) {
  91.     int done;
  92.     int len;
  93.  
  94.     len = fread(Buff, 1, BUFFSIZE, stdin);
  95.     if (ferror(stdin)) {
  96.       fprintf(stderr, "Read error\n");
  97. #ifdef AMIGA  
  98.       CloseLibrary((APTR) ExpatBase);;
  99. #endif
  100.       exit(-1);
  101.     }
  102.     done = feof(stdin);
  103.  
  104.     if (! XML_Parse(p, Buff, len, done)) {
  105.       fprintf(stderr, "Parse error at line %d:\n%s\n",
  106.           XML_GetCurrentLineNumber(p),
  107.           XML_ErrorString(XML_GetErrorCode(p)));
  108. #ifdef AMIGA  
  109.       CloseLibrary((APTR) ExpatBase);;
  110. #endif
  111.       exit(-1);
  112.     }
  113.  
  114.     if (done)
  115.       break;
  116.   }
  117.   
  118. #ifdef AMIGA  
  119.   CloseLibrary((APTR) ExpatBase);
  120.   exit(0);
  121. #else
  122.   return 0;
  123. #endif
  124.   
  125. }  /* End of main */
  126.  
  127.